#抓取https://sportapi.fastball2.com/v1/match/matchResultList
import time
import datetime
import requests

import random
import logging
from pb.comfuc import Comfuc
from pb.ref import Ref
from pb.sport import Sport
class CheckResult:

    def fetch_match_list(self):

        url = "https://sportapi.fastball2.com/v1/match/matchResultList"


        User_Agent_list = [
            {
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0'
            }, {
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
            }
        ]

        User_Agent = random.choice(User_Agent_list)

        # 请求头
        headers =  {
            "Accept": "application/json, text/plain, */*",
            "Accept-Encoding": "gzip, deflate, br, zstd",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Content-Type": "application/json;charset=UTF-8",
            "Content-Length":"22",
            "Host": "sportapi.fastball2.com",
            "Origin": "https://test.f66b88sport.com",
            "Referer": "https://test.f66b88sport.com/",
            "Sec-Fetch-Dest": "empty",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Site": "cross-site",
            "Sec-Ch-Ua-mobile": "?0",
            "Sec-Ch-Ua-platform": '"Windows"'
        }
        headers['User-Agent'] = User_Agent.get('user-agent')
        headers['Sec-Ch-Ua'] =  '"Not/A)Brand";v="8", "Chromium";v="126", "Microsoft Edge";v="126"'
        # 更新后的请求体 (payload)
#        today_timestamp = self.get_today_timestamp()
#        tomrrow_timestamp = today_timestamp + 86399999
        sixago = self.get_rounded_timestamp(25)
        oneage = self.get_rounded_timestamp(1)
        payload = {
            "beginTime": sixago,
            "endTime": oneage,
            "sportId":1,
            "languageType": 'ENG' # CMN or ENG
        }

        try:
            # 发起 POST 请求
            session = requests.Session()
            response = session.post(url, headers=headers, json=payload, timeout=10)

            # 检查响应状态码
            if response.status_code == 200:
                session.close()
               # print("请求成功，返回的数据如下：")
                #print(response.json() )  # 输出 JSON 数据
                return response.json()

            else:
                logging.warning(f"请求失败，状态码：{response.status_code}")
                logging.warning("响应内容：", response.text)
        except Exception as e:
            logging.error(f"请求过程中发生错误：{e}")
            time.sleep(6)


    def get_scors(self):
        scors_dict = {}
        list = self.fetch_match_list()['data']
        for dt in list:
            nsg = dt['nsg']
            if len(nsg) :
                for scs in nsg:
                    if scs['pe'] == 1001 and scs['tyg'] == 5:
                        #print('-------')
                        #print(dt['id'])
                        score = f"{scs['sc'][0]}-{scs['sc'][1]}"
                        #print(score)
                        key = dt['id']
                        scors_dict[key] = score
                        #print('#######')
        return scors_dict



    def get_rounded_timestamp(self,hours_ago):
        now = datetime.datetime.now()
        past_time = now - datetime.timedelta(hours=hours_ago)
        rounded_time = past_time.replace(minute=0, second=0, microsecond=0)
        return int(time.mktime(rounded_time.timetuple()))*1000

if __name__ == "__main__":
    checkResult = CheckResult()
    scors_dict = checkResult.get_scors()
    print(scors_dict)


